Imports System.Text.RegularExpressions
Imports System.Web

Namespace DotNetNuke.Modules.ActiveForums
    Public Class CodeParser
        Public Shared Function ParseCode(ByVal sCode As String) As String
            Dim objRegEx As Regex
            Dim sOut As String
            sCode = Replace(sCode, "[", "&#91;")
            sCode = Replace(sCode, "]", "&#93;")
            sCode = Regex.Replace(sCode, "(&#91;CODE&#93;)", "[CODE]", RegexOptions.IgnoreCase)
            sCode = Regex.Replace(sCode, "(&#91;\/CODE&#93;)", "[/CODE]", RegexOptions.IgnoreCase)
            'objRegEx = New Regex("\[CODE([^>]*)\](.*?)\[/CODE\]", RegexOptions.Singleline Or RegexOptions.IgnoreCase)
            objRegEx = New Regex("\[CODE([^>]*)\]((.|\n)*?)\[/CODE\]", RegexOptions.IgnoreCase)
            'sCode = HandleBrackets(sCode)

            'If objRegEx.IsMatch(sCode) Then

            'Else
            '    objRegEx = New Regex("[CODE](.*?)[/CODE]", RegexOptions.Singleline Or RegexOptions.IgnoreCase)
            'End If
            Dim Matches As MatchCollection

            Matches = objRegEx.Matches(sCode)
            Dim myMatch As Match
            sOut = sCode
            Dim tmp As String = String.Empty
            Dim codeSnip As String = String.Empty
            For Each myMatch In Matches
                codeSnip = myMatch.Result("$2")
                codeSnip = codeSnip.Replace("&quot;", """")
                codeSnip = HttpUtility.HtmlEncode(codeSnip)
                tmp = "<pre>" & codeSnip & "</pre>"
                If InStr(myMatch.Result("$1"), "vb") > 0 Then
                    'Dim objCode As New Manoli.Utils.CSharpFormat.VisualBasicFormat
                    'objCode.Alternate = False
                    'objCode.EmbedStyleSheet = False
                    'tmp = objCode.FormatCode(codeSnip)
                    tmp = HandleBrackets(tmp)
                    sCode = sCode.Replace(myMatch.Value, "<div class=""afcodeblock"">" & tmp & "</div>")
                ElseIf InStr(myMatch.Result("$1"), "html") > 0 Then
                    'Dim objCode As New Manoli.Utils.CSharpFormat.HtmlFormat
                    'objCode.Alternate = False
                    'objCode.EmbedStyleSheet = False
                    'tmp = objCode.FormatCode(codeSnip)
                    tmp = HandleBrackets(tmp)
                    sCode = sCode.Replace(myMatch.Value, "<div class=""afcodeblock"">" & tmp & "</div>")
                ElseIf InStr(myMatch.Result("$1"), "csharp") > 0 Then
                    'Dim objCode As New Manoli.Utils.CSharpFormat.CSharpFormat
                    'objCode.Alternate = False
                    'objCode.EmbedStyleSheet = False
                    'tmp = objCode.FormatCode(codeSnip)
                    tmp = HandleBrackets(tmp)
                    sCode = sCode.Replace(myMatch.Value, "<div class=""afcodeblock"">" & tmp & "</div>")
                ElseIf InStr(myMatch.Result("$1"), "script") > 0 Then
                    'Dim objCode As New Manoli.Utils.CSharpFormat.JavaScriptFormat
                    'objCode.Alternate = False
                    'objCode.EmbedStyleSheet = False
                    'tmp = objCode.FormatCode(codeSnip)
                    tmp = HandleBrackets(tmp)
                    sCode = sCode.Replace(myMatch.Value, "<div class=""afcodeblock"">" & tmp & "</div>")
                Else
                    'Dim objCode As New Manoli.Utils.CSharpFormat.HtmlFormat
                    'objCode.Alternate = False
                    'objCode.EmbedStyleSheet = False
                    'tmp = objCode.FormatCode(codeSnip)
                    tmp = HandleBrackets(tmp)
                    sCode = sCode.Replace(myMatch.Value, "<div class=""afcodeblock"">" & tmp & "</div>")
                End If
            Next
            'sCode = sCode.Replace("</pre>", vbCrLf & "</pre>")
            'sOut = "<div class=""afcodeblock"">" & sCode & "</div>"
            Return HandleBrackets(sCode)
        End Function
        Private Shared Function HandleBrackets(ByVal sCode As String) As String
            sCode = sCode.Replace("&#91;", "[")
            sCode = sCode.Replace("&amp;#91;", "[")
            sCode = sCode.Replace("&#93;", "]")
            sCode = sCode.Replace("&amp;#93;", "]")

            Return sCode
        End Function
    End Class
End Namespace
